package com.mmloo.common.message;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;

/**
 * 用于表示一个操作结果，toJson方法返回该message的JSON字符串
 *
 */
public class Message implements Serializable {
	
	private boolean success;
	
	private String msg;
	
	private Object data;
	
	public Message(){}
	
	public Message(boolean success, String msg){
		this.success = success;
		this.msg = msg;
	}
	
	public Message(boolean success, String msg, Object data){
		this.success = success;
		this.msg = msg;
		this.data = data;
	}
	
	/**
	 * 快速创建成功返回data的message，success==true
	 * 
	 * @param data 
	 */
	public Message(Object data){
		this.success = true;
		this.data = data;
	}

	public boolean isSuccess() {
		return this.success;
	}
	
	public String getMsg() {
		return msg;
	}
	
	public Object getData() {
		return data;
	}

	public Message success() {
		this.success = true;
		return this;
	}

	public Message failed() {
		this.success = false;
		return this;
	}

	public Message msg(String msg) {
		this.msg = msg;
		return this;
	}
	
	public Message data(Object data) {
		this.data = data;
		return this;
	}
	
	public String toJson(){
		return JSON.toJSONString(this);
	}
}
